﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

public partial class AutoScan_AutoOutbox : System.Web.UI.Page
{
    #region 引用亿美DLL
    [DllImport("EUCPComm.dll", EntryPoint = "SendSMS")]  //即时发送
    public static extern int SendSMS(string sn, string mn, string ct, string priority);

    [DllImport("EUCPComm.dll", EntryPoint = "SendSMSEx")]  //即时发送(扩展)
    public static extern int SendSMSEx(string sn, string mn, string ct, string addi, string priority);

    [DllImport("EUCPComm.dll", EntryPoint = "SendScheSMS")]  // 定时发送
    public static extern int SendScheSMS(string sn, string mn, string ct, string ti, string priority);

    [DllImport("EUCPComm.dll", EntryPoint = "SendScheSMSEx")]  // 定时发送(扩展)
    public static extern int SendScheSMSEx(string sn, string mn, string ct, string ti, string addi, string priority);
    #endregion

    //短信下行实体类
    public static Cisoft.Cisoft3G.Model.OutboxMO model = new Cisoft.Cisoft3G.Model.OutboxMO();
    //短信下行业务逻辑类
    public static Cisoft.Cisoft3G.BLL.OutboxBO bll = new Cisoft.Cisoft3G.BLL.OutboxBO();
    //亿美产品序列号
    public static string EmeyKey = System.Configuration.ConfigurationSettings.AppSettings["EmeyKey"].ToString();
    //亿美产品密码
    public static string EmeyPwd = System.Configuration.ConfigurationSettings.AppSettings["EmeyPassword"].ToString();

    public static string ss = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButton1_Click1(sender, e);
    }

    /// <summary>
    /// 启动服务
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //lblTbPre.Text = "服务运行中...";
        //txt_Time.Enabled = false;
        //lb_EndService.Enabled = true;
        //lb_StartService.Enabled = false;


    }
    /// <summary>
    /// 停止服务
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lb_EndService_Click(object sender, EventArgs e)
    {
        //lblTbPre.Text = "请输入服务间隔：";
        //txt_Time.Enabled = true;
        //lb_EndService.Enabled = false;
        //lb_StartService.Enabled = true;
    }

    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        //日程提醒记录扫描若时间状态达到则立即发送并写入已发表
        RemindToSentbox();




        //发送短信
        Cisoft.Cisoft3G.BLL.SentBO Sentbll = new Cisoft.Cisoft3G.BLL.SentBO();
        Cisoft.Cisoft3G.Model.SentMO Sentmodel = new Cisoft.Cisoft3G.Model.SentMO();

        Cisoft.Cisoft3G.BLL.SmsTypeBO SmsTypebll = new Cisoft.Cisoft3G.BLL.SmsTypeBO();
        Cisoft.Cisoft3G.Model.SmsTypeMO SmsTypemodel = new Cisoft.Cisoft3G.Model.SmsTypeMO();


        //循环扫描待发短信表，获取待发时间小于当前时间的记录，立即发送短信
        string strEmeyError = "";
        Cisoft.Cisoft3G.BLL.OutboxBO Outboxbll = new Cisoft.Cisoft3G.BLL.OutboxBO();
        List<Cisoft.Cisoft3G.Model.OutboxMO> OutboxmodelList = Outboxbll.GetModelList(" OB_SendTime <= '" + DateTime.Now.ToString() + "'");
        foreach (Cisoft.Cisoft3G.Model.OutboxMO Outboxmo in OutboxmodelList)
        {
            //发送立即短信
            try
            {
                strEmeyError = EmeySmsSend(Outboxmo.OB_Recive.Trim(), Outboxmo.OB_SMContent.Trim());
            }
            catch (Exception ex)
            {
                lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "outboxGuid  " + Outboxmo.OB_SmsID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                continue;
            }

            //发送短信成功后转移到sent表
            if (strEmeyError == "发送成功")
            {
                //新增sent表记录，删除outbox表记录
                Sentmodel.ST_SentID = Sentbll.GetMaxId();
                Sentmodel.ST_Recive = Outboxmo.OB_Recive.Trim();
                Sentmodel.ST_SentTime = DateTime.Now;
                Sentmodel.ST_SmsContent = Outboxmo.OB_SMContent.Trim();
                Sentmodel.ST_SmsID = Outboxmo.OB_SmsID;
                Sentmodel.ST_SmsType = "普通";

                SmsTypemodel = SmsTypebll.GetModel(int.Parse(Outboxmo.RT_Type_FK.ToString()));
                if (SmsTypemodel != null)
                {
                    Sentmodel.ST_SmsType = SmsTypemodel.ST_TypeName;
                }

                try
                {
                    Sentbll.Add(Sentmodel);
                    Outboxbll.Delete(Outboxmo.OB_OutBoxiD);
                }
                catch (Exception exupdate)
                {
                    lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "outboxGuid" + Outboxmo.OB_SmsID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                    continue;
                }
            }
            else
            {
                lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "outboxGuid" + Outboxmo.OB_SmsID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                continue;
            }
        }
    }

    /// <summary>
    /// 将字符串转换为时间
    /// </summary>
    /// <param name="input"></param>
    public static DateTime GetTime(string input)
    {
        return Convert.ToDateTime(input.Substring(0, 4) + "-" + input.Substring(4, 2) + "-" +
                input.Substring(6, 2) + " " + input.Substring(8, 2) + ":" + input.Substring(10, 2) + ":" + input.Substring(12, 2));
    }

    #region 亿美短信发送方法（含传入参数）
    private string EmeySmsSend(string strRecives, string strContent)
    {
        int result = SendSMS(EmeyKey, strRecives, strContent, "5");
        if (result == 1)
            return "发送成功";
        else if (result == 101)
            return "网络故障";
        else if (result == 102)
            return "其它故障";
        else if (result == 0)
            return "失败";
        else if (result == 100)
            return "序列号码为空或无效";
        else if (result == 107)
            return "手机号码为空或者超过1000个";
        else if (result == 108)
            return "手机号码分割符号不正确";
        else if (result == 109)
            return "部分手机号码不正确，已删除，其余手机号码被发送";
        else if (result == 110)
            return "短信内容为空或超长（70个汉字）";
        else if (result == 201)
            return "计费失败，请充值";
        else
            return "其他故障值：" + result.ToString();
    }
    #endregion

    #region 日程提醒记录扫描若时间状态达到则立即发送并写入已发表
    public void RemindToSentbox()
    {
        //发送短信
        Cisoft.Cisoft3G.BLL.SentBO Sentbll = new Cisoft.Cisoft3G.BLL.SentBO();
        Cisoft.Cisoft3G.Model.SentMO Sentmodel = new Cisoft.Cisoft3G.Model.SentMO();

        //循环扫描日程提醒表，获取 提醒时间小于当前时间，且 状态 RM_Status为 0的记录，立即发送短信




        string strEmeyError = "";
        Cisoft.Cisoft3G.BLL.RemindBO Remindbll = new Cisoft.Cisoft3G.BLL.RemindBO();

        string strwhereforneed = "";
        //开始时间小于等于当前时间 类型从不 处理标识0
        //开始时间小于等于当前时间 结束时间大于等于当前时间 类型结束之前 处理标识0
        //(RM_StartTime <= getdate() and RM_EndType=0 and RM_Status=0)or (RM_StartTime <= getdate() and RM_EndType=1 and RM_Status=0 and RM_EndTime >= getdate())
        strwhereforneed += " (RM_StartTime <= '" + DateTime.Now.ToString() + "'";
        strwhereforneed += " and RM_EndType=0 and RM_Status=0)";
        strwhereforneed += " or";
        strwhereforneed += " (RM_StartTime <= '" + DateTime.Now.ToString() + "'";
        strwhereforneed += " and RM_EndType=1 and RM_Status=0";
        strwhereforneed += " and RM_EndTime >= '" + DateTime.Now.ToString() + "')";


        List<Cisoft.Cisoft3G.Model.RemindMO> RemindmodelList = Remindbll.GetModelList(strwhereforneed);
        foreach (Cisoft.Cisoft3G.Model.RemindMO Remindmo in RemindmodelList)
        {
            foreach (string s in Remindmo.RM_Recive.Trim().TrimEnd(',').Split(','))
            {
                //根据频率类型进行发送
                switch (Remindmo.RM_RateType.ToString())
                {
                    case "0":
                        #region 发送立即短信 写入已发表
                        //发送立即短信
                        try
                        {
                            //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                            strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                        }
                        catch (Exception ex)
                        {
                            lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                            continue;
                        }

                        //发送短信成功后转移到sent表
                        if (strEmeyError == "发送成功")
                        {
                            //新增sent表记录，删除outbox表记录
                            Sentmodel.ST_SentID = Sentbll.GetMaxId();
                            //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                            Sentmodel.ST_Recive = s;
                            Sentmodel.ST_SentTime = DateTime.Now;
                            Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                            Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                            Sentmodel.ST_SmsType = "日程提醒";
                            try
                            {
                                Sentbll.Add(Sentmodel);
                                //已处理 重置为1
                                Remindmo.RM_Status = 1;
                                Remindbll.Update(Remindmo);
                            }
                            catch (Exception exupdate)
                            {
                                lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                continue;
                            }
                        }
                        else
                        {
                            lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                        }
                        #endregion
                        //lblRateType.Text = "不重复";
                        break;
                    case "1":
                        #region 发送立即短信 写入已发表
                        //发送立即短信
                        try
                        {
                            //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                            strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                        }
                        catch (Exception ex)
                        {
                            lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                            continue;
                        }

                        //发送短信成功后转移到sent表
                        if (strEmeyError == "发送成功")
                        {
                            //新增sent表记录，删除outbox表记录
                            Sentmodel.ST_SentID = Sentbll.GetMaxId();
                            //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                            Sentmodel.ST_Recive = s;
                            Sentmodel.ST_SentTime = DateTime.Now;
                            Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                            Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                            Sentmodel.ST_SmsType = "日程提醒";
                            try
                            {
                                Sentbll.Add(Sentmodel);
                                //已处理 重置为1
                                Remindmo.RM_Status = 1;
                                Remindbll.Update(Remindmo);
                            }
                            catch (Exception exupdate)
                            {
                                lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                continue;
                            }
                        }
                        else
                        {
                            lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                        }
                        #endregion

                        //lblRateType.Text = "每天 " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                        break;
                    case "2":

                        DateTime nowtime = DateTime.Now;

                        bool tag = false;
                        switch (nowtime.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("7") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Monday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("1") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Tuesday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("2") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Wednesday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("3") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Thursday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("4") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Friday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("5") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                            case DayOfWeek.Saturday:
                                if (Remindmo.RM_StandardWeeks.IndexOf("6") >= 0)
                                {
                                    tag = true;
                                }
                                break;
                        }

                        if (tag)
                        {
                            #region 发送立即短信 写入已发表
                            //发送立即短信
                            try
                            {
                                //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                                strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                            }
                            catch (Exception ex)
                            {
                                lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                                continue;
                            }

                            //发送短信成功后转移到sent表
                            if (strEmeyError == "发送成功")
                            {
                                //新增sent表记录，删除outbox表记录
                                Sentmodel.ST_SentID = Sentbll.GetMaxId();
                                //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                                Sentmodel.ST_Recive = s;
                                Sentmodel.ST_SentTime = DateTime.Now;
                                Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                                Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                                Sentmodel.ST_SmsType = "日程提醒";
                                try
                                {
                                    Sentbll.Add(Sentmodel);
                                    //已处理 重置为1
                                    Remindmo.RM_Status = 1;
                                    Remindbll.Update(Remindmo);
                                }
                                catch (Exception exupdate)
                                {
                                    lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                    continue;
                                }
                            }
                            else
                            {
                                lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                            }
                            #endregion
                        }

                        //lblRateType.Text += selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                        break;
                    case "3":

                        if (Convert.ToDateTime(Remindmo.RM_StandardTime).Day == DateTime.Now.Day)
                        {
                            #region 发送立即短信 写入已发表
                            //发送立即短信
                            try
                            {
                                //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                                strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                            }
                            catch (Exception ex)
                            {
                                lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                                continue;
                            }

                            //发送短信成功后转移到sent表
                            if (strEmeyError == "发送成功")
                            {
                                //新增sent表记录，删除outbox表记录
                                Sentmodel.ST_SentID = Sentbll.GetMaxId();
                                //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                                Sentmodel.ST_Recive = s;
                                Sentmodel.ST_SentTime = DateTime.Now;
                                Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                                Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                                Sentmodel.ST_SmsType = "日程提醒";
                                try
                                {
                                    Sentbll.Add(Sentmodel);
                                    //已处理 重置为1
                                    Remindmo.RM_Status = 1;
                                    Remindbll.Update(Remindmo);
                                }
                                catch (Exception exupdate)
                                {
                                    lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                    continue;
                                }
                            }
                            else
                            {
                                lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                            }
                            #endregion
                        }

                        //lblRateType.Text = "每月 第";
                        //lblRateType.Text += " " + time3.ToString("dd天");
                        //lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                        break;
                    case "4":

                        if (Convert.ToDateTime(Remindmo.RM_StandardTime).ToString("MM-dd") == DateTime.Now.ToString("MM-dd"))
                        {
                            #region 发送立即短信 写入已发表
                            //发送立即短信
                            try
                            {
                                //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                                strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                            }
                            catch (Exception ex)
                            {
                                lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                                continue;
                            }

                            //发送短信成功后转移到sent表
                            if (strEmeyError == "发送成功")
                            {
                                //新增sent表记录，删除outbox表记录
                                Sentmodel.ST_SentID = Sentbll.GetMaxId();
                                //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                                Sentmodel.ST_Recive = s;
                                Sentmodel.ST_SentTime = DateTime.Now;
                                Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                                Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                                Sentmodel.ST_SmsType = "日程提醒";
                                try
                                {
                                    Sentbll.Add(Sentmodel);
                                    //已处理 重置为1
                                    Remindmo.RM_Status = 1;
                                    Remindbll.Update(Remindmo);
                                }
                                catch (Exception exupdate)
                                {
                                    lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                    continue;
                                }
                            }
                            else
                            {
                                lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                            }
                            #endregion
                        }



                        //lblRateType.Text = "每年 的";
                        //lblRateType.Text += " " + time4.ToString("MM月dd日");
                        //lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                        break;
                    default:
                        #region 发送立即短信 写入已发表
                        //发送立即短信
                        try
                        {
                            //strEmeyError = EmeySmsSend(Remindmo.RM_Recive.Trim(), Remindmo.RM_SMContent.Trim());
                            strEmeyError = EmeySmsSend(s, Remindmo.RM_SMContent.Trim());
                        }
                        catch (Exception ex)
                        {
                            lblResult.Text += "发送短信异常，系统异常信息" + ex.Message.ToString() + "错误编号" + strEmeyError + "RM_RemindID  " + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "<br/>";
                            continue;
                        }

                        //发送短信成功后转移到sent表
                        if (strEmeyError == "发送成功")
                        {
                            //新增sent表记录，删除outbox表记录
                            Sentmodel.ST_SentID = Sentbll.GetMaxId();
                            //Sentmodel.ST_Recive = Remindmo.RM_Recive.Trim();
                            Sentmodel.ST_Recive = s;
                            Sentmodel.ST_SentTime = DateTime.Now;
                            Sentmodel.ST_SmsContent = Remindmo.RM_SMContent.Trim();
                            Sentmodel.ST_SmsID = System.Guid.NewGuid().ToString();
                            Sentmodel.ST_SmsType = "日程提醒";
                            try
                            {
                                Sentbll.Add(Sentmodel);
                                //已处理 重置为1
                                Remindmo.RM_Status = 1;
                                Remindbll.Update(Remindmo);
                            }
                            catch (Exception exupdate)
                            {
                                lblResult.Text += "更新数据库异常" + "错误原因" + exupdate.Message.ToString() + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                                continue;
                            }
                        }
                        else
                        {
                            lblResult.Text += "发送短信异常" + "错误编号" + strEmeyError + "RM_RemindID" + Remindmo.RM_RemindID.ToString() + "时间" + DateTime.Now.ToString() + "'<br/>";
                        }
                        #endregion
                        //lblRateType.Text = "不重复";
                        break;

                }
            }
        }
    }
    #endregion
}
